package com.cyys.modules.system.controller.file;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.ql.AR;
import com.cyys.common.utils.StringUtil;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.system.model.file.FileDir;
import com.cyys.modules.system.model.file.FileEntry;
import com.cyys.modules.system.service.file.FileDirService;
import com.cyys.modules.system.service.file.FileEntryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
@RequestMapping("/sys/file")
public class FileDirAction extends BaseAction {

	@Resource
	private FileDirService fileDirService;

	@Resource
	private FileEntryService fileEntryService;

	/******
	 * 文件管理器
	 * @param selectType 0:无选择按钮，代表此页面不是从其他页面的文件上传功能点进来的，而是直接打开系统功能里的文件管理页面，1：应用于单文件选择，2：应用于多文件选择
	 * @param objId
	 * @param showFileType,显示的文件类型(若不为空只支持单个)，1：图片，2：音视频，3：文档,为空则显示全部（还有4：其他）
	 * @param storeMode 音視頻存儲方式，oss/vod，默認oss，vod即阿里視頻點播
	 */
	@GetMapping("/manage")
	public String list(Integer selectType, String objId, String showFileType, String storeMode, Model model) {
		//selectType选择器类型
		if(selectType == null)
			selectType = 0;
		model.addAttribute("selectType", selectType);
		model.addAttribute("objId", objId);
		model.addAttribute("showFileType", showFileType);
		model.addAttribute("storeMode", storeMode);
		Map<String,String> selectTypeMap= new LinkedHashMap(){{
			put("1","图片");
			put("2","音视频");
			put("3","文档");
			put("4","其他");
		}};
		if(StringUtil.isNotBlank(showFileType)){
			Map<String,String> retSelectTypeMap= new LinkedHashMap();
			retSelectTypeMap.put(showFileType, selectTypeMap.get(showFileType));
			model.addAttribute("selectTypeMap", retSelectTypeMap);
		}else{
			model.addAttribute("selectTypeMap", selectTypeMap);
		}
		return "module/system/file/file-manage";
	}

	/************文件目录相关操作**************/
	@ResponseBody
	@PostMapping("/del/deleteById")
	public AR deleteById(String id)  {
		return fileDirService.deleteById(id);
	}

	@GetMapping("/addDir")
	public String addInput(FileDir fileDir,Model model)  {
		if (fileDir != null && fileDir.getId() != null) {
			model.addAttribute("fileDir", fileDirService.getById(fileDir.getId()));
		}
		return "module/system/file/fileDir-input";
	}

	@ResponseBody
	@PostMapping("/saveDir")
	public AR save(FileDir fileDir)  {
		fileDir.setUserId(getUser().getId());
		if(StringUtil.isBlank(fileDir.getId())){
			return fileDirService.save(fileDir);
		}else{
			return fileDirService.update(fileDir);
		}
	}

	@ResponseBody
	@PostMapping("/getAll")
	public AR getAll()  {
		QueryWrapper<FileDir> qw = new QueryWrapper<>();
		FileDir fileDir = new FileDir();
		fileDir.setUserId(getUser().getId());
		qw.setEntity(fileDir);
		qw.orderByAsc("date_create");
		return AR.ok(fileDirService.listAll(qw),"获取成功");
	}


	/************文件相关操作**************/
	@ResponseBody
	@PostMapping("/fileList")
	public AR fileList(FileEntry fileEntry, Page page)  {
		if(StringUtil.isEmpty(fileEntry.getDirId()))
			fileEntry.setDirId("0");
		if(StringUtil.isBlank(fileEntry.getStoreMode())){//默认只显示oss的，以防错乱
			fileEntry.setStoreMode("oss");
		}else if("all".equals(fileEntry.getStoreMode())){//查询全部
			fileEntry.setStoreMode("");
		}
		return AR.ok(fileEntryService.list(page , new QueryWrapper(fileEntry),true),"获取成功") ;
	}

	@ResponseBody
	@PostMapping("/delFile")
	public AR delFile(String id)  {
		return fileEntryService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/delFiles")
	public AR delFiles(String ids)  {
		return fileEntryService.deleteBatchIds(ids);
	}

	@GetMapping("/renameFileInput")
	public String renameFileInput(FileEntry fileEntry,Model model)  {
		if (fileEntry != null && fileEntry.getId() != null) {
			model.addAttribute("fileEntry", fileEntryService.getById(fileEntry.getId()));
		}
		return "module/system/file/renameFile-input";
	}

	@ResponseBody
	@PostMapping("/renameFile")
	public AR renameFile(FileEntry fileEntry)  {
		if(StringUtil.isBlank(fileEntry.getId())){
			return fileEntryService.save(fileEntry);
		}
		return fileEntryService.update(fileEntry);
	}

	@GetMapping("/moveFileInput")
	public String moveFileInput(String ids,Model model)  {
		model.addAttribute("ids", ids);
		QueryWrapper<FileDir> qw = new QueryWrapper<>();
		FileDir fileDir = new FileDir();
		fileDir.setUserId(getUser().getId());
		qw.setEntity(fileDir);
		qw.orderByAsc("date_create");
		model.addAttribute("fileDirList",fileDirService.listAll(qw));
		return "module/system/file/moveFile";
	}

	@ResponseBody
	@PostMapping("/moveFile")
	public AR moveFile(String id, String dirId)  {
		return fileEntryService.moveFile(id, dirId);
	}

	@ResponseBody
	@PostMapping("/moveFiles")
	public AR moveFiles(String ids, String dirId)  {
		return fileEntryService.moveFiles(ids, dirId);
	}


	/**************回收站相关操作***********/
	@ResponseBody
	@PostMapping("/fileRecycleList")
	public AR fileRecycleList(FileEntry fileEntry, Page page)  {
		if(StringUtil.isEmpty(fileEntry.getDirId()))
			fileEntry.setDirId("0");
		fileEntry.setUserId(getUser().getId());
		return AR.ok(fileEntryService.listRecycle(page , fileEntry),"获取成功") ;
	}

}
